import { axiosInstance } from 'src/boot/axios'
import CommonConfig from 'src/assets/js/config'

const baseUrlPlantPlan = CommonConfig.planningUrl + '/v1'

import qs from 'qs'

const qsConfig = {
  arrayFormat: 'repeat',
  skipNulls: true,
  format: 'RFC3986'
}

// 强制关闭
export function orderForceClose (id) {
  const newUrl = `${baseUrlPlantPlan}/workshop_plannings/${id}:force_close`

  return axiosInstance.post(newUrl).then(resp => {
    return Promise.resolve(resp)
  })
}

// 根据生产计划 列举工单状态
export function workOrdersStatus (workshopPlanningId) {
  const newUrl = `${baseUrlPlantPlan}/workshop_plannings/${workshopPlanningId}/work_orders/status`

  return axiosInstance.get(newUrl).then(resp => {
    return Promise.resolve(resp)
  })
}

// 列举车间计划列表
export function getPlanList (endAfter, endBefore, keyword, pageNum, pageSize, searchBy, startAfter, startBefore, statusCode, sortBy, productId, isShowClosedPlanning) {
  const newUrl = `${baseUrlPlantPlan}/workshop_plannings`

  let reqData = {
    params: {
      endAfter: endAfter || undefined,
      endBefore: endBefore || undefined,
      keyword: keyword || undefined,
      pageNum,
      pageSize,
      searchBy: searchBy || undefined,
      startAfter: startAfter || undefined,
      startBefore: startBefore || undefined,
      statusCode,
      sort: sortBy || undefined,
      productId,
      isShowClosedPlanning
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 删除单个计划
export function deletePlan (planningId) {
  const newUrl = `${baseUrlPlantPlan}/workshop_plannings/${planningId}`

  return axiosInstance.delete(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

// 批量删除
export function batchDeletion (ids) {
  const newUrl = `${baseUrlPlantPlan}/workshop_plannings`

  let config = {
    params: {
      ids
    },
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }

  return axiosInstance.delete(newUrl, config).then(resp => {
    return Promise.resolve(resp)
  })
}

// 列举产品下拉框
export function getMaterials () {
  const newUrl = `process/v1/products/simple`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

// 列举产品下拉框
export function getSearchMaterials (searchKey) {
  const newUrl = `process/v1/products:searchKey`
  let reqData = {
    params: {
      searchKey
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 根据产品获取工艺路线
 * @param productId 产品id
 * inUseOnly: 工艺路线是否在用
 * @returns {Promise<AxiosResponse<any>>}
 */
export function getRouting (params) {
  const newUrl = `process/v1/process_route_list`
  if (!params.productId) {
    return Promise.reject()
  }

  // let reqData = {
  //   params: {
  //     productId,
  //     version,
  //     inUseOnly: false
  //   }
  // }

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}
/**
 * 根据产品获取工艺路线
 * @param productId 产品id
 * inUseOnly: 工艺路线是否在用
 * @returns {Promise<AxiosResponse<any>>}
 */
export function getRoutingStepList (productId) {
  const newUrl = `process/v1/process_route_list`

  let reqData = {
    params: {
      productId,
      inUseOnly: false
    }
  }

  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 新增计划
export function addPlan (query) {
  const url = `${baseUrlPlantPlan}/workshop_plannings`

  return axiosInstance.post(url, query).then(resp => {
    return Promise.resolve(resp)
  })
}

// 编辑计划
export function editPlan (query) {
  const url = `${baseUrlPlantPlan}/workshop_plannings/${query.planningId}`

  let params = {
    ...query
  }

  return axiosInstance.put(url, params).then(resp => {
    return Promise.resolve(resp)
  })
}

// 锁定车间计划
export function batchLock (ids) {
  const newUrl = `${baseUrlPlantPlan}/workshop_plannings:batchLock`

  // let config = {
  //   ids,
  //   paramsSerializer: function (ids) {
  //     return qs.stringify(ids, qsConfig)
  //   }
  // }

  return axiosInstance.post(newUrl, { ids }).then(resp => {
    return Promise.resolve(resp)
  })
}

// 解锁车间计划
export function batchUnLock (ids) {
  const newUrl = `${baseUrlPlantPlan}/workshop_plannings:batchUnlock`

  // let config = {
  //   ids,
  //   paramsSerializer: function (ids) {
  //     return qs.stringify(ids, qsConfig)
  //   }
  // }

  return axiosInstance.post(newUrl, { ids }).then(resp => {
    return Promise.resolve(resp)
  })
}

// 冻结车间计划
export function batchFreeze (ids) {
  const newUrl = `${baseUrlPlantPlan}/workshop_plannings:batchFreeze`

  // let config = {
  //   ids,
  //   paramsSerializer: function (ids) {
  //     return qs.stringify(ids, qsConfig)
  //   }
  // }

  return axiosInstance.post(newUrl, { ids }).then(resp => {
    return Promise.resolve(resp)
  })
}

// 解冻车间计划
export function batchUnfreeze (ids) {
  const newUrl = `${baseUrlPlantPlan}/workshop_plannings:batchUnfreeze`

  // let config = {
  //   ids,
  //   paramsSerializer: function (ids) {
  //     return qs.stringify(ids, qsConfig)
  //   }
  // }

  return axiosInstance.post(newUrl, { ids }).then(resp => {
    return Promise.resolve(resp)
  })
}

// 列举操作记录
export function getLog (planningId, pageNum, pageSize) {
  const newUrl = `${baseUrlPlantPlan}/workshop_plannings/${planningId}/logs`

  let reqData = {
    params: {
      pageNum,
      pageSize
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 分解车间计划
export function batchDisassemble (ids) {
  const newUrl = `${baseUrlPlantPlan}/workshop_plannings:batchSplit`

  // let config = {
  //   ids,
  //   paramsSerializer: function (ids) {
  //     return qs.stringify(ids, qsConfig)
  //   }
  // }

  return axiosInstance.post(newUrl, { ids }).then(resp => {
    return Promise.resolve(resp)
  })
}

// 解除分解车间计划
export function batchUndoSplit (ids) {
  const newUrl = `${baseUrlPlantPlan}/workshop_plannings:batchUndoSplit`

  // let config = {
  //   ids,
  //   paramsSerializer: function (ids) {
  //     return qs.stringify(ids, qsConfig)
  //   }
  // }

  return axiosInstance.post(newUrl, { ids }).then(resp => {
    return Promise.resolve(resp)
  })
}

// 列举一个车间计划下的全部工序计划
export function getProcessPlannings (workshopPlanningId) {
  const newUrl = `${baseUrlPlantPlan}/workshop_plannings/${workshopPlanningId}/process_plannings`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

// 通过产品查工艺路线
// export function getProcessRouteLst (productId) {
//   const newUrl = `/process/v1/process_route_list`
//
//   let reqData = {
//     params: {
//       inUseOnly: true,
//       productId
//     }
//   }
//
//   return axiosInstance.get(newUrl, reqData).then((res) => {
//     return Promise.resolve(res)
//   })
// }

// 编辑工序计划
export function editProcessPlan (query) {
  const newUrl = `${baseUrlPlantPlan}/process_plannings/${query.id}`

  let params = {
    ...query
  }

  return axiosInstance.put(newUrl, params).then(resp => {
    return Promise.resolve(resp)
  })
}

// 删除工序计划
export function deleteProcessPlan (id) {
  const newUrl = `${baseUrlPlantPlan}/process_plannings/${id}`

  return axiosInstance.delete(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

// 调整工序计划顺序
export function sortProcessPlan (id1, id2) {
  const newUrl = `${baseUrlPlantPlan}/process_plannings/sort`

  let params = {
    id1,
    id2
  }

  return axiosInstance.put(newUrl, params).then(resp => {
    return Promise.resolve(resp)
  })
}

// 向车间计划添加工序计划
export function addProcessPlan (query) {
  const newUrl = `${baseUrlPlantPlan}/workshop_plannings/${query.workshopPlanningId}/process_plannings`

  return axiosInstance.post(newUrl, query).then(resp => {
    return Promise.resolve(resp)
  })
}

// 向车间计划添加工序计划
export function getPlanningNumber () {
  const newUrl = `${baseUrlPlantPlan}/workshop_plannings/work_planning_number_generate`

  return axiosInstance.get(newUrl).then(resp => {
    return Promise.resolve(resp)
  })
}

// 判断计划是否重复
export function isRepeat (params) {
  const newUrl = `${baseUrlPlantPlan}/workshop_plannings:repeatCheck`

  return axiosInstance.get(newUrl, { params }).then(resp => {
    return Promise.resolve(resp)
  })
}
